package com.education.module.classes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.Class;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class ClassServices extends BaseServices implements IDao<Class>{

	@Override
	public List<Class> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,class_name,professional_id,grade_id,remark FROM class");
		
		StringBuffer sqlCase = new StringBuffer();
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("className") && !"".equals((String)searchParams.get("className"))){
				sqlCase.append(lp).append(" class_name like ? ");
				argsList.add("%" + searchParams.get("className") + "%");
				lp = " and ";
			}
		}
		sql.append(sqlCase.toString());
		sql.append(" order by class_name");
		Object[] args = argsList.toArray();
		List<Class> list = dao.query(pageSQL(sql.toString(),page),args,new ClassRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from class " + sqlCase.toString(),args));
		return list;
	}
	
	public List<Class> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,class_name,professional_id,grade_id,remark FROM class");
		
		List<Class> list = dao.query(sql.toString(),new ClassRowmapper());
		return list;
	}

	@Override
	public int save(Class obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("insert into class ( "); 
 sql.append("id,class_name,professional_id,grade_id,remark ");  
 sql.append(" ) values(?,?,?,?,?) "); 
 Object[] args = {obj.getId(),obj.getClassName(),obj.getProfessionalId(),obj.getGradeId(),obj.getRemark() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public Class findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,class_name,professional_id,grade_id,remark FROM class ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new ClassRowmapper());
	}

	@Override
	public void update(Class obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update class "); 
 sql.append("set  "); 
 sql.append("class_name=?,professional_id=?,grade_id=?,remark=? where id=?  "); 
 Object[] args = {obj.getClassName(),obj.getProfessionalId(),obj.getGradeId(),obj.getRemark(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from class where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class ClassRowmapper implements RowMapper<Class> {
		@Override
		public Class mapRow(ResultSet rs, int arg1) throws SQLException {
			Class obj = new Class();
			obj.setId(rs.getInt("id")); 
obj.setRemark(rs.getString("remark")); 
obj.setGradeId(rs.getInt("grade_id")); 
obj.setProfessionalId(rs.getInt("professional_id")); 
obj.setClassName(rs.getString("class_name")); 

			return obj;
		}
	}

}
